package com.nero.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nero.bean.PO.*;
import com.nero.bean.VO.ItemAddVO;
import com.nero.bean.VO.ItemParamVO;
import com.nero.bean.VO.ItemVO;
import com.nero.common.pojo.EUTreeNode;
import com.nero.common.utils.IDUtils;
import com.nero.common.utils.TimeUtil;
import com.nero.dao.TbItemCatMapper;
import com.nero.dao.TbItemDescMapper;
import com.nero.dao.TbItemMapper;
import com.nero.dao.TbItemParamMapper;
import com.nero.service.IItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Nero on 18/8/6.
 */
@Service
public class ItemServiceImpl implements IItemService {
    @Resource
    TbItemMapper tbItemMapper;
    @Resource
    TbItemCatMapper tbItemCatMapper;
    @Resource
    TbItemParamMapper tbItemParamMapper;
    @Resource
    TbItemDescMapper tbItemDescMapper;

    @Override
    public PageInfo<ItemVO> findAllItemVO(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ItemVO> list = tbItemMapper.findAllItemVO();
        PageInfo<ItemVO> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<ItemParamVO> findAllItemParamVO(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ItemParamVO> list = tbItemParamMapper.findAllItemParamVO();
        PageInfo<ItemParamVO> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<EUTreeNode> findTreeNodes(long parent_id) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria c = example.createCriteria();
        c.andParentIdEqualTo(parent_id);
        List<TbItemCat> itemList = tbItemCatMapper.selectByExample(example);
        List<EUTreeNode> nodeList = new ArrayList<>();
        for (TbItemCat tbItemCat : itemList) {
            EUTreeNode euTreeNode = new EUTreeNode();
            euTreeNode.setId(tbItemCat.getId());
            euTreeNode.setText(tbItemCat.getName());
            euTreeNode.setState(tbItemCat.getIsParent() ? "closed" : "open");
            nodeList.add(euTreeNode);
        }
        return nodeList;
    }

    @Override
    public void addItem(ItemAddVO itemAddVO) {
        long id = IDUtils.genItemId();
        Date date = new Date();

        // tb_item表
        TbItem tbItem = new TbItem();
        tbItem.setId(id);
        tbItem.setTitle(itemAddVO.getTitle());
        tbItem.setSellPoint(itemAddVO.getSellPoint());
        tbItem.setPrice(itemAddVO.getPrice());
        tbItem.setNum(itemAddVO.getNum());
        tbItem.setBarcode(itemAddVO.getBarcode());
        tbItem.setImage(itemAddVO.getImage());
        tbItem.setCid(itemAddVO.getCid());
        tbItem.setStatus(new Byte("1"));
        tbItem.setCreated(date);
        tbItem.setUpdated(date);

        // tb_item_desc表
        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemId(id);
        tbItemDesc.setItemDesc(itemAddVO.getDesc());
        tbItemDesc.setCreated(date);
        tbItemDesc.setUpdated(date);

        // 向表里添加数据
        tbItemMapper.insert(tbItem);
        tbItemDescMapper.insert(tbItemDesc);
    }
}
